package com.yehuishou.controller;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.HUser;
import com.yehuishou.service.AdminService;
import com.yehuishou.service.PermissionService;

/**
 * 管理员管理
 * @author zxb
 *
 */
@Controller
public class AdminController {
	
	@Autowired
	private PermissionService permissionService;
	@Autowired
	private AdminService adminService;

	//查询所有管理员
	@RequestMapping("adminlist")
	public String adminList(@RequestParam(required=true,defaultValue="1") Integer pageNum,
			@RequestParam(required=true,defaultValue="10") Integer pageSize, Model model) {
		PageHelper.startPage(pageNum, pageSize);
		List<HUser> list = adminService.findAll();
		PageInfo<HUser> p=new PageInfo<HUser>(list);
		model.addAttribute("page", p);
	    model.addAttribute("list",list);
		return "content-6_3";
	}
	
	@RequestMapping(value="/adminfind")
	public String adminFindP(@RequestParam(required=true,defaultValue="1") Integer pageNum,
			@RequestParam(required=true,defaultValue="10") Integer pageSize,String uphone , String nickname, Model model) {
		PageHelper.startPage(pageNum, pageSize);
		//nickname = new String(nickname.getBytes("ISO8859-1"), "UTF-8");
		List<HUser> list = adminService.findBy(uphone,nickname);
		PageInfo<HUser> p=new PageInfo<HUser>(list);
		List<String> fan = new ArrayList<String>();
		fan.add(uphone);
		fan.add(nickname);
		model.addAttribute("page", p);
	    model.addAttribute("list",list);
	    model.addAttribute("fan", fan);
		return "content-6_3";
	}
	
	@RequestMapping("admin")
	public String admin() {
		return"content-6_3_add";
	}
	
	//添加管理员
	@RequestMapping("perAdd")
	public String adminAdd(HUser huser , Integer[] perid) {
		//添加用户
		adminService.adminAdd(huser);
		Integer h_uid=huser.getH_uid();
		//添加权限
		permissionService.perUpdate(perid , h_uid);
		return "redirect:adminlist";
	}
	
	//删除
	@ResponseBody
	@RequestMapping("admindel")
	public boolean adminDel(Integer h_uid) {
		adminService.adminDel(h_uid);
		return true;
	}
	
	@ResponseBody
	@RequestMapping("checkname")
	public Map<String, Object> checkname(String uname) {
		Map<String,Object> map = new HashMap<>();
		HUser huser = adminService.findByUName(uname);
		if(huser!=null) {
			map.put("msg", "用户名已存在");
			map.put("success", false);
		}else {
			map.put("success", true);
		}
		return map;
	}
	
}
